﻿// Inner Fire 游戏引擎库
// Material - 材质
//
// Copyright (c) 2025 by 尔西大帝. All rights reserved.
//
// 所属层级：应用层(4)
// 材质的资源。
//
// Author: 尔西大帝
// Email: 2670613871@qq.com
// Created: 2025-01-14

#include "material.h"
#include <enum_parser.h>

namespace ifire::game {

Material::Material(Assets* manager, const XmlNode& node)
    : Resource(manager, node("Name"))
    , material_(nullptr)
    , type(ReadType(node("Type")))
    , albedo(node("Albedo"))
    , normal(node("Normal"))
    , tint(node("Tint", XMFLOAT4(1.0f, 1.0f, 1.0f, 1.0f)))
    , fresnel_r0(node("FresnelR0", XMFLOAT3(0.01f, 0.01f, 0.01f)))
    , roughness(node("Roughness")) {}

dx::RenderLayer Material::GetRenderLayer() const {
  switch (type) {
  case MaterialType::Normal:
    return dx::RenderLayer::Opaque;
  case MaterialType::Hollow:
    return dx::RenderLayer::AlphaTest;
  case MaterialType::Alpha:
    return dx::RenderLayer::AlphaTransparent;
  case MaterialType::Transparency:
    return dx::RenderLayer::Transparent;
  }
  return dx::RenderLayer::Opaque;
}

MaterialType Material::ReadType(ikit::sstr name) {

  static std::pair<ikit::sstr, MaterialType> MATERIAL_TYPE_ENUMS[] = {
      {"Normal", MaterialType::Normal}, //
      {"Hollow", MaterialType::Hollow}, //
      {"Alpha", MaterialType::Alpha}, //
      {"Transparency", MaterialType::Transparency},
  };
  static EnumParser<MaterialType> materialTypeParser(
      MATERIAL_TYPE_ENUMS, (int)MaterialType::Count);
  return materialTypeParser.Get(name, MaterialType::Normal);
}

} // namespace ifire::game